package com.hxh.wechat.activity

import android.os.Bundle
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.hxh.wechat.R
import com.hxh.wechat.database.UserDbHelper
import com.hxh.wechat.model.RegisterUser
import com.hxh.wechat.util.DBUtil

class RegisterActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.register)
        val numberYear = findViewById<NumberPicker>(R.id.number_picker_year)
        val numberMonth = findViewById<NumberPicker>(R.id.number_picker_month)
        val numberDay = findViewById<NumberPicker>(R.id.number_picker_day)
        numberYear.minValue = 1900
        numberYear.maxValue = 2300
        numberYear.value = 2000
        numberMonth.minValue = 1
        numberMonth.maxValue = 12
        numberMonth.value = 1
        numberDay.minValue = 1
        numberDay.maxValue = 31
        numberDay.value = 1
        findViewById<ImageView>(R.id.registerBack).setOnClickListener {
            finish()
        }
        val schoolData = listOf(
            "广东东软学院", "广东中医药大学", "广东工业大学", "华南理工大学",
            "华南师范大学", "华南农业大学"
        )
        val adpter = ArrayAdapter(this, android.R.layout.simple_list_item_1, schoolData)
        findViewById<AutoCompleteTextView>(R.id.school).setAdapter(adpter);
        val cityData = listOf(
            "广州", "佛山", "深圳", "东莞",
            "中山", "珠海"
        )
        val cityAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, cityData)
        findViewById<Spinner>(R.id.city).adapter = cityAdapter;

        //注册存储数据到sqllite

        findViewById<Button>(R.id.regButton).setOnClickListener {
            val username = findViewById<EditText>(R.id.username).text.toString()
            val password = findViewById<EditText>(R.id.password).text.toString()
            val passwordTwo = findViewById<EditText>(R.id.passwordTwo).text.toString()
            val radioGroup = findViewById<RadioGroup>(R.id.sex)
            val sex = if (radioGroup.checkedRadioButtonId == R.id.sexBoy) "男" else "女"
            val year = findViewById<NumberPicker>(R.id.number_picker_year).value
            val month = findViewById<NumberPicker>(R.id.number_picker_month).value
            val day = findViewById<NumberPicker>(R.id.number_picker_day).value
            val school = findViewById<AutoCompleteTextView>(R.id.school).text.toString()
            val city = findViewById<Spinner>(R.id.city).selectedItem.toString()
            if (passwordTwo != password) {
                DBUtil.popMsg("密码不一致", con = this);
                return@setOnClickListener;
            }
            val user: RegisterUser =
                RegisterUser(username, password, sex, "$year-$month-$day", school, city)
            UserDbHelper(this).insert(user);
        }
    }
}